package com.itheima.elasticsearch;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

import java.net.InetAddress;

public class Demo01 {
    /** 创建索引库(blog1)、类型(article)、文档(document) */
    @Test
    public void test01() throws Exception {
        // 1. 创建ES传输客户端对象
        TransportClient transportClient = new PreBuiltTransportClient(Settings.EMPTY);
        // 1.1 添加传输地址对象
        transportClient.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"),9300));
        // 2. 创建内容构建对象
        XContentBuilder builder = XContentFactory.jsonBuilder()
                .startObject()
                .field("id",1)
                .field("title","elasticsearch 搜索服务")
                .field("content","ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎。")
                .endObject();
        // 3. 执行索引库、类型、文档
        transportClient.prepareIndex("blog1","article","1")
                .setSource(builder).get(); // 执行get请求
        // 4. 释放资源
        transportClient.close();
    }

    /** 查询blog1索引库中的article表 (查询全部) */
    @Test
    public void test02() throws Exception{
        // 1. 创建ES传输客户端对象
        TransportClient transportClient = new PreBuiltTransportClient(Settings.EMPTY);
        // 1.1 添加传输地址对象
        transportClient.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"),9300));
        // 2. 执行搜索，得到搜索响应对象
        SearchResponse searchResponse = transportClient.prepareSearch("blog1")//设置索引库
                .setTypes("article")//设置类型(表)
                .setQuery(QueryBuilders.matchAllQuery())//设置查询语法
                .get();
        // 3. 获取搜索结果
        SearchHits hits = searchResponse.getHits();
        System.out.println("总命中数：" + hits.totalHits);

        // 4. 迭代搜索结果
        for (SearchHit hit : hits) {
            System.out.println("JSON字符串：" + hit.getSourceAsString());
            System.out.println("id：" + hit.getSourceAsMap());
            System.out.println("title：" + hit.getSourceAsMap());
            System.out.println("content：" + hit.getSourceAsMap());
        }
        //释放资源
        transportClient.close();
    }

    /** 查询blog1索引库中的article表 (字符串查询) */
    @Test
    public void test03() throws Exception {
        //创建ES传输客户端对象
        TransportClient transportClient = new PreBuiltTransportClient(Settings.EMPTY);
        //添加输出地址对象
        transportClient.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"),9300));
        //执行搜索，得到搜索响应对象
        SearchResponse searchResponse = transportClient.prepareSearch("blog1")
                .setTypes("article").setQuery(QueryBuilders.queryStringQuery("全垒打")).get();
        //获取搜索结果
        SearchHits hits = searchResponse.getHits();
        System.out.println("总命中数：" + hits.totalHits);

        //迭代搜索结果
        for (SearchHit hit : hits) {
            System.out.println("JSON字符串：" + hit.getSourceAsString());
            System.out.println("id：" + hit.getSourceAsMap().get("id"));
            System.out.println("title：" + hit.getSourceAsMap().get("title"));
            System.out.println("content：" + hit.getSourceAsMap().get("content"));
        }
        //释放资源
        transportClient.close();
    }

    /** 查询blog1索引库中的article表 (词条查询) */
    @Test
    public void test04() throws Exception {
        //创建ES传输客户端对象
        TransportClient transportClient = new PreBuiltTransportClient(Settings.EMPTY);
        //添加传输地址对象
        transportClient.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"),9300));
        //执行搜索，得到搜索响应对象(termQuery: 一个整体，不分词)
        SearchResponse searchResponse = transportClient.prepareSearch("blog1")
                .setTypes("article").setQuery(QueryBuilders.termQuery("content","搜索")).get();
        //获取搜索结果
        SearchHits hits = searchResponse.getHits();
        System.out.println("总命中数：" + hits.totalHits);

        //迭代搜索结果
        for (SearchHit hit : hits) {
            System.out.println("JSON字符串：" + hit.getSourceAsString());
            System.out.println("id：" + hit.getSourceAsMap().get("id"));
            System.out.println("title：" + hit.getSourceAsMap().get("title"));
            System.out.println("content：" + hit.getSourceAsMap().get("content"));
        }
        //释放资源
        transportClient.close();
    }

    /** 查询blog1索引库中的article表 (模糊查询) */
    @Test
    public void test05() throws Exception {
        //创建ES传输客户端对象
        TransportClient transportClient = new PreBuiltTransportClient(Settings.EMPTY);
        //添加传输地址对象
        transportClient.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"),9300));
        //执行搜索，得到搜索响应对象(通匹符 * 星号)
        SearchResponse searchResponse = transportClient.prepareSearch("blog1").setTypes("article")
                .setQuery(QueryBuilders.wildcardQuery("title","*lastic*")).get();
        //获取搜索结果
        SearchHits hits = searchResponse.getHits();
        System.out.println("总命中数：" + hits.totalHits);

        //迭代搜索结果
        for (SearchHit hit : hits) {
            System.out.println("JSON字符串：" + hit.getSourceAsString());
            System.out.println("id：" + hit.getSourceAsMap().get("id"));
            System.out.println("title：" + hit.getSourceAsMap().get("title"));
            System.out.println("content：" + hit.getSourceAsMap().get("content"));
        }
        //释放资源
        transportClient.close();
    }
}
